home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 08 net data types / nettypesdemo / classes.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  3.0 KB  |  108 lines

  1. ' The PointXY class demonstrates how an object can override the ToString to
  2. ' provide a textual representation of its current state.
  3.  
  4. Class PointXY
  5.     Public X, Y As Double
  6.  
  7.     Sub New(ByVal X As Double, ByVal Y As Double)
  8.         Me.X = X
  9.         Me.Y = Y
  10.     End Sub
  11.  
  12.     ' Return the X-Y coordinates in the format "(X,Y)"
  13.     Overrides Function ToString() As String
  14.         Return "(" & X.ToString & "," & Y.ToString & ")"
  15.     End Function
  16.  
  17. End Class
  18.  
  19. ' a class that implements IFormattable interface
  20.  
  21. Class PointXY2
  22.     Implements IFormattable
  23.  
  24.     Public X, Y As Double
  25.  
  26.     Sub New(ByVal X As Double, ByVal Y As Double)
  27.         Me.X = X
  28.         Me.Y = Y
  29.     End Sub
  30.  
  31.     ' Return the X-Y coordinates in the format "(X,Y)"
  32.     Overrides Function ToString() As String
  33.         Return "(" & X.ToString & "," & Y.ToString & ")"
  34.     End Function
  35.  
  36.     Private Function Format(ByVal FormatStr As String, _
  37.         ByVal fp As IFormatProvider) As String _
  38.         Implements IFormattable.ToString
  39.  
  40.         If FormatStr = "" Then
  41.             ' If no formatting is passed, use default formatting
  42.             Return Me.ToString
  43.         Else
  44.             ' Otherwise replace X and Y characters with actual coordinates.
  45.             Return Replace(Replace(FormatStr, "X", X.ToString), _
  46.                 "Y", Y.ToString)
  47.         End If
  48.     End Function
  49.  
  50. End Class
  51.  
  52. ' The MetricConverter class demonstrates the IFormatProvider and ICustomFormatter interfaces
  53.  
  54. Class MetricConverter
  55.     Implements IFormatProvider
  56.     Implements ICustomFormatter
  57.  
  58.     ' The only method of the IFormatProvider interface
  59.     Private Function GetFormat(ByVal Service As System.Type) _
  60.         As Object Implements IFormatProvider.GetFormat
  61.  
  62.         If Service.Name = "ICustomFormatter" Then
  63.             ' Return this instance if called from the String.Format method.
  64.             Return Me
  65.         Else
  66.             ' Return Nothing in all other cases.
  67.             Return Nothing
  68.         End If
  69.     End Function
  70.  
  71.     ' The only method of the ICustomFormatter interface
  72.     Function Format(ByVal formatStr As String, ByVal arg As Object, _
  73.         ByVal fp As IFormatProvider) As String _
  74.         Implements ICustomFormatter.Format
  75.  
  76.         If formatStr = "" Then
  77.             ' No format string, return the argument.
  78.             Return arg.ToString
  79.         ElseIf formatStr = "in" Then
  80.             ' Convert inches to meters.
  81.             Return (CDbl(arg) * 0.0254).ToString
  82.         ElseIf formatStr = "fe" Then
  83.             ' Convert feet to meters.
  84.             Return (CDbl(arg) * 0.33).ToString
  85.         End If
  86.     End Function
  87. End Class
  88.  
  89. ' two Enums for our experiments
  90.  
  91. ' This Enum defines the data type accepted for a 
  92. ' value entered by the end user.
  93. Enum DataEntry As Short
  94.     IntegerNumber
  95.     FloatingNumber
  96.     CharString
  97.     DateTime
  98. End Enum
  99.  
  100. <Flags()> _
  101. Enum ValidDataEntry As Short
  102.     None = 0              ' Always define an enum value = 0.
  103.     IntegerNumber = 1
  104.     FloatingNumber = 2
  105.     CharString = 4
  106.     DateTime = 8
  107. End Enum
  108.